package com.fragment;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.adapter.base.ViewHolder;
import com.adapter.base.recyclerview.CommonAdapter;
import com.adapter.base.recyclerview.OnRecyclerItemClickListener;
import com.bumptech.glide.Glide;
import com.util.R;
import com.util.ToastUtil;
import com.view.recyclerViewDivider.RecyclerViewGridLayoutDivider;
import com.view.recyclerViewDivider.RecyclerViewLinearLayoutDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/4/6.
 */
public class RecyclerViewFragment extends Fragment {
    @Bind(R.id.rv_RecyclerView)
    RecyclerView mRecyclerView;
    private View view = null;
    private List<Integer> randomHeight;
    private List<String> imgUrlList;
    private ProgressDialog dialog;
    private boolean lock = true;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.fragment_recyclerview, null);
            ButterKnife.bind(this, view);
            Bundle arg = getArguments();
            int mode = arg.getInt("mode");
            initImgUrlList();
            initRecyclerView(mode);
        }
        return view;
    }

    private void initImgUrlList() {
        imgUrlList = new ArrayList<>();
        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");


    }

    private void initRecyclerView(int mode) {
        mRecyclerView.setHasFixedSize(true);
        CommonAdapter adapter = new CommonAdapter<String>(getActivity(), R.layout.item_photo_list, imgUrlList) {
            @Override
            public void convert(ViewHolder holder, final String s) {
                Glide.with(getActivity())
                        .load(s)
                        .placeholder(R.mipmap.loading) // can also be a drawable
                        .error(R.mipmap.error) // will be displayed if the image cannot be loaded
                        .into((ImageView) holder.getView(R.id.iv_photoList_staggeredGrid));
            }
        };

        switch (mode) {
            case 0: {
                final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
                mRecyclerView.setLayoutManager(linearLayoutManager);//这里用线性显示 类似于listview
                mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (linearLayoutManager.findLastVisibleItemPosition() == imgUrlList.size() - 1) {
                            if(lock){
                                lock = false;
                                showLoadingDialog();
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        mRecyclerView.getAdapter().notifyDataSetChanged();
                                        dialog.dismiss();
                                        lock=true;
                                    }
                                }, 3000);
                            }
                        }
                    }


                });
                mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
                    @Override
                    public void onItemClick(RecyclerView.ViewHolder vh) {
                        ToastUtil.showCenterToast("我是:" + vh.getAdapterPosition());
                    }
                });
                mRecyclerView.setAdapter(adapter);
                mRecyclerView.addItemDecoration(new RecyclerViewLinearLayoutDivider(RecyclerViewLinearLayoutDivider.VERTICAL_LIST, getResources().getDrawable(R.drawable.sp_divider_listview)));
                break;
            }
            case 1:
                final GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
                mRecyclerView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
                mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);
                        if (gridLayoutManager.findLastVisibleItemPosition() == imgUrlList.size() - 1) {
                            if(lock){
                                lock = false;
                                showLoadingDialog();
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
                                        imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
                                        mRecyclerView.getAdapter().notifyDataSetChanged();
                                        dialog.dismiss();
                                        lock=true;
                                    }
                                }, 3000);
                            }
                        }

                    }
                });
                mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
                    @Override
                    public void onItemClick(RecyclerView.ViewHolder vh) {
                        ToastUtil.showBottomToast("我是:" + vh.getAdapterPosition());
                    }
                });
                mRecyclerView.setAdapter(adapter);
                mRecyclerView.addItemDecoration(new RecyclerViewGridLayoutDivider(getResources().getDrawable(R.drawable.sp_divider_gridview)));
                break;
            case 2: {
                randomHeight = new ArrayList<>();
                initRandomHeight();
                final StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, OrientationHelper.VERTICAL);
                mRecyclerView.setLayoutManager(staggeredGridLayoutManager);//这里用线性宫格显示 类似于瀑布流
//                mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
//                    @Override
//                    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//                        super.onScrollStateChanged(recyclerView, newState);
//                        int lastVisibleItemsPos[] = staggeredGridLayoutManager.findLastVisibleItemPositions(null);
//                        for (int i = 0; i < lastVisibleItemsPos.length; i++) {
//                            LogUtil.i("test", lastVisibleItemsPos[i]);
//                            if (lastVisibleItemsPos[i] == imgUrlList.size() - 1) {
//                                if(lock){
//                                    lock = false;
//                                    showLoadingDialog();
//                                    new Handler().postDelayed(new Runnable() {
//                                        @Override
//                                        public void run() {
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/5.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/1.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/2.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/3.jpg");
//                                            imgUrlList.add("http://www.pelway.cn/images/slider/4.jpg");
//                                            for (int i = 0; i < 14; i++) {
//                                                randomHeight.add((int) (100 + Math.random() * 300));
//                                            }
//                                            mRecyclerView.getAdapter().notifyDataSetChanged();
//                                            dialog.dismiss();
//                                            lock=true;
//                                        }
//                                    }, 3000);
//                                }
//                            }
//                                break;
//                            }
//                        }
//                });
                mRecyclerView.addOnItemTouchListener(new OnRecyclerItemClickListener(mRecyclerView) {
                    @Override
                    public void onItemClick(RecyclerView.ViewHolder vh) {
                        ToastUtil.showBottomToast("我是:" + vh.getAdapterPosition());
                    }
                });

                mRecyclerView.setAdapter(new CommonAdapter<String>(getActivity(), R.layout.item_photo_list, imgUrlList) {
                    @Override
                    public void convert(ViewHolder holder, final String s) {
                        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, randomHeight.get(holder.getAdapterPosition()));
                        holder.getView(R.id.iv_photoList_staggeredGrid).setLayoutParams(params);

                        Glide.with(getActivity())
                                .load(s)
                                .placeholder(R.mipmap.loading) // can also be a drawable
                                .error(R.mipmap.error) // will be displayed if the image cannot be loaded
                                .into((ImageView) holder.getView(R.id.iv_photoList_staggeredGrid));


                    }
                });
                mRecyclerView.addItemDecoration(new RecyclerViewGridLayoutDivider(getResources().getDrawable(R.drawable.sp_divider_gridview)));
                break;
            }

        }

    }

    private void showLoadingDialog() {
        if (dialog != null && dialog.isShowing()) {
            return;
        } else if (dialog != null && !dialog.isShowing()) {
            dialog.show();
            return;
        }
        dialog = new ProgressDialog(getActivity());
        dialog.setTitle("数据加载");
        dialog.setMessage("正在加载...");
        dialog.show();
    }

//    //LinearLayoutManager局部刷新
//    private void linearLayoutManagerPartRefresh(int type, RecyclerView recyclerView, RecyclerView.LayoutManager layoutManager) {
//        int firstPos = 0;
//        int lastPos = 0;
//        if (type == 0) {
//            firstPos = ((LinearLayoutManager) layoutManager).findFirstVisibleItemPosition();
//            lastPos = ((LinearLayoutManager) layoutManager).findLastVisibleItemPosition();
//        } else if (type == 1) {
//            firstPos = ((GridLayoutManager) layoutManager).findFirstVisibleItemPosition();
//            lastPos = ((GridLayoutManager) layoutManager).findLastVisibleItemPosition();
//        }
//        for (int i = firstPos; i <= lastPos; i++) {
//            recyclerView.getAdapter().notifyItemChanged(i);
//        }
//
//    }

    private void initRandomHeight() {
        for (int i = 0; i < imgUrlList.size(); i++) {
            randomHeight.add((int) (100 + Math.random() * 300));
        }
    }

}



